/*
 *  queue.c
 *  Asset modeling
 *
 *  Created by Peeter Meos on 27.02.11.
 *  Copyright 2011 NC3A CAT1. All rights reserved.
 *
 */

#ifndef QUEUE_H
	#define QUEUE_H
	#include "queue.h"
#endif

#include <stdlib.h>

// Push new element to the end

T_CASUALTY *push_cas(T_CASUALTY *queue, T_CASUALTY *casualty)
{
	
	if (queue == NULL)
	{
		queue = casualty;
	}else{
		queue->next = push_cas(queue->next, casualty);
	}
	return queue;
}

// Pop topmost member

T_CASUALTY *pop_cas(T_CASUALTY **queue)
{
	T_CASUALTY *x = *queue;
	
	if (x != NULL)
	{
		*queue = x->next;
		x->next = NULL;
	}
	
	return x;
}

char cas_stack_len(void *queue)
{
	char i;
	T_CASUALTY	*x;
	
	i = 0;
	x = queue;
	
	while (!( x == NULL))
	{
		i++;
		x = x->next;
	}
	
	return i;
}

void push_event(T_EVENT **queue, T_EVENT *event)
{
	T_EVENT *tmp;
	
	if (*queue == NULL)
	{
		*queue = event;
	}else{
		tmp = *queue;
		if(tmp->time < event->time)
			push_event((T_EVENT **)&(tmp->next), event);
		else{	
			tmp = *queue;
			*queue = event;
			(*queue)->next = tmp;			
		}
	}
	return;
}

// Pop topmost member

T_EVENT *pop_event(T_EVENT **queue)
{
	T_EVENT *x = *queue;
	
	if (*queue != NULL)
	{
		*queue = x->next;
		x->next = NULL;
	}
	
	return x;
}